package com.zhu.transaction.controller;


import com.google.gson.Gson;
import com.sun.corba.se.impl.oa.toa.TOA;
import com.zhu.transaction.pojo.*;
import com.zhu.transaction.pojo.state.BackState;
import com.zhu.transaction.pojo.state.R;
import com.zhu.transaction.service.FavoriteService;
import com.zhu.transaction.service.OrderService;
import com.zhu.transaction.service.ProductService;
import com.zhu.transaction.service.UserService;
import com.zhu.transaction.utils.MyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2021-01-07
 */
@Controller
@RequestMapping("/person")
@Api(tags = "个人中心接口", description = "我的资料、我的收藏、我的发布、我的订单")
public class PersonController {

    @Autowired
    ProductService productService;

    @Autowired
    UserService userService;

    @Autowired
    FavoriteService favoriteService;

    @Autowired
    OrderService orderService;

    @RequestMapping("/myRelease")
    @ApiOperation(value = "我的发布", hidden = true)
    public String myRelease(Integer curPage, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("myProductsNum", productService.getProductNum(user.getId(), new ProductCondition()));
        model.addAttribute("curPage", curPage);
        model.addAttribute("myProducts", productService.getMyProducts(user.getId(), curPage));
        return "/person/myProduct";
    }

    @RequestMapping("/myFavorite")
    @ApiOperation(value = "我的收藏", hidden = true)
    public String myFavorite(@RequestParam(value = "curPage", required = false) Integer curPage, Model model, HttpSession session) {
        if (curPage == null) curPage = 1;
        User user = (User) session.getAttribute("user");
        model.addAttribute("favorites", favoriteService.getFavoriteProductsByPage(user.getId(), curPage));
        model.addAttribute("curPage", curPage);
        return "/person/myFavorite";
    }

    @RequestMapping("/myOrder")
    @ApiOperation(value = "我的订单", hidden = true)
    public String myOrder(@RequestParam(value = "curPage", required = false) Integer curPage, Model model, HttpSession session) {
        if (curPage == null) curPage = 1;
        User user = (User) session.getAttribute("user");
        model.addAttribute("favorites", favoriteService.getFavoriteProductsByPage(user.getId(), curPage));
        model.addAttribute("curPage", curPage);
        return "/person/myOrder";
    }

    @PostMapping("/updatePersonInfo")
    @ApiOperation(value = "我的资料", hidden = true)
    public String updatePersonInfo(User user, HttpSession session) {
        User curLoginUser = (User) session.getAttribute("user");
        user.setId(curLoginUser.getId());
        String password = user.getPassword();
        if (password != null && !password.equals("")) {
            user.setPassword(MyUtils.string2MD5(password));
        }
        userService.updateUserInfo(user);
        session.setAttribute("user", userService.getOneUserById(curLoginUser.getId()));
        return "redirect:/person/update";
    }


    @ResponseBody
    @PostMapping("/addFavorite")
    @ApiOperation(value = "添加收藏商品")
    public R addFavorite(String productId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        BackState backState = favoriteService.favoriteProduct(user.getId(), productId);
        if (backState == BackState.NO_EXIST) {
            return R.error().message("该商品已不存在");
        }
        if (backState == BackState.HAS_EXIST)
            return R.error().message("该商品已收藏");
        return backState == BackState.SUCCESS ? R.ok().message("已收藏") : R.error().message("收藏失败");
    }

    @ResponseBody
    @PostMapping("/cancelFavorite")
    @ApiOperation(value = "取消收藏商品")
    public R cancelFavorite(String[] favoriteIds, Integer isCancel, HttpSession session) {
        if (isCancel == 1) {
            BackState backState = favoriteService.cancelFavoriteProduct(favoriteIds);
            return backState == BackState.SUCCESS ? R.ok().message("已取消收藏").data("isCancel", 0) : R.error().message("取消收藏失败").data("isCancel", 1);
        } else {
            User user = (User) session.getAttribute("user");
            BackState backState = favoriteService.favoriteProduct(user.getId(), favoriteIds[0]);
            return backState == BackState.SUCCESS ? R.ok().message("已收藏").data("isCancel", 1) : R.error().message("收藏失败").data("isCancel", 0);
        }

    }

    @PostMapping("/cancelFavorite2")
    @ApiOperation(value = "取消收藏商品", hidden = true)
    public String cancelFavorite2(String[] favoriteIds, HttpSession session) {
        BackState backState = favoriteService.cancelFavoriteProduct(favoriteIds);
        if (backState == BackState.SUCCESS) {
            session.setAttribute("backMessage", new Gson().toJson(R.ok().message("取消成功")));
        } else {
            session.setAttribute("backMessage", new Gson().toJson(R.ok().message("取消失败")));
        }
        return "redirect:/person/myFavorite";
    }

    @ResponseBody
    @GetMapping("/orderList")
    @ApiOperation(value = "获取我的订单")
    public R getOrderList(String userId, Boolean isPurchase) {
        List<OrderItem> orderList = orderService.getOrderList(userId, isPurchase);
        return orderList.size() == 0 ? R.ok().data("count", 0).message(isPurchase ? "你还没有购买相关商品" : "你还没有卖出相关商品") :
                R.ok().data("item", orderList).code(0).data("count", orderList.size());
    }

}

